package com.hotel.controller;

import com.hotel.dao.HomeMapper;
import com.hotel.pojo.Home;
import com.hotel.pojo.Yuding;
import com.hotel.service.HomeServiceImpl;
import com.hotel.service.YudingServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Yuding")
public class    YudingController {

    @Autowired
    YudingServiceImpl YudingService;
    @Autowired
    HomeMapper homeService;

    @RequestMapping("/add")
    public String add(@RequestBody Yuding Yuding){
        ModelAndView mv = new ModelAndView();
        YudingService.addYuding(Yuding);
//        mv.setViewName("suc_v");
//        return mv;
        return "redirect:/Yuding/list";
    }

    @RequestMapping("/delete")
    public String delete(int id){
        Yuding yuding = YudingService.queryYudingById(id);
        Home home = new Home();
        if (yuding!=null&&yuding.equals("")){
            home.setState("空房");
            home.setNum(yuding.getRoomNumber());
            homeService.updateHomeByhomeId(home);
        }
        YudingService.deleteYudingById(id);
        return "redirect:/Yuding/list";
    }

    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView();
        List<Yuding> YudingList=YudingService.queryAllYuding();
        mv.addObject("list",YudingList);
        mv.setViewName("Yuding_list");
        return mv;
    }

    @RequestMapping("/update1")
    public ModelAndView update1(int id){
        ModelAndView mv = new ModelAndView();
        Yuding Yuding = YudingService.queryYudingById(id);
        mv.addObject("h",Yuding);
        mv.setViewName("Yuding_update");
        return mv;
    }

    @RequestMapping("/update2")
    public String update2(Yuding v){
        YudingService.updateYudingById(v);
        return ("redirect:/Yuding/list");
    }

    @RequestMapping("/find")
    public ModelAndView find(String findByPhone ){
        ModelAndView mv = new ModelAndView();
        Yuding Yuding = YudingService.queryYudingByPhone(findByPhone);
        List<Yuding> YudingList = new ArrayList<Yuding>();
        YudingList.add(Yuding);
        if (Yuding==null){
            YudingList=YudingService.queryAllYuding();
            mv.addObject("error","未查询出结果");
        }
        mv.addObject("list",YudingList);
        mv.setViewName("Yuding_list");
        return mv;
    }
}

